﻿using MediatR;
using Production_Set.API.Application.Commands.Supplier;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.Procurement
{
    public class OrderLinesDeleteHandler : IRequestHandler<OrderLinesDeleteCommand, Result>
    {
        /// <summary>
        /// 联系人信息
        /// </summary>
        private readonly IBaseRepository<OrderLines> OrderLinesRepository;

        private readonly ILogger<OrderLinesDeleteHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="OrderLinesRepository"></param>
        /// <param name="logger"></param>
        public OrderLinesDeleteHandler(IBaseRepository<OrderLines> OrderLinesRepository, ILogger<OrderLinesDeleteHandler> logger)
        {
            this.OrderLinesRepository = OrderLinesRepository;
            this.logger = logger;
        }
        public async Task<Result> Handle(OrderLinesDeleteCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Delete };

            try
            {
                string sql = $"update OrderLines set IsDelete=1 where OrderLineId={request.OrderLineId}";

                var list = OrderLinesRepository.Update(sql, result);

                if (list > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Msg = "删除成功";
                    logger.LogInformation("订单行信息删除");
                }
            }
            catch (Exception ex)
            {
                logger.LogError("订单行信息删除失败" + ex.Message);
                throw;
            }

            return result;
        }
    }
}
